//
//  MemberRoamingVC.m
//  SelfDriving
//
//  Created by apple on 16/1/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MemberRoamingVC.h"
#import "MemberRoamingSVC.h"
//#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
@interface MemberRoamingVC ()<MAMapViewDelegate,AMapSearchDelegate,UITableViewDelegate,UITableViewDataSource>
{
    //    BMKMapView *_mapView;
    //    BMKLocationService *_locService;
    MAPointAnnotation *pointAnnotation;
    CLLocationCoordinate2D coor;
    
}
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) IBOutlet UIView *fview;
@property (strong, nonatomic) IBOutlet MAMapView *map;
@property (weak, nonatomic) IBOutlet UIView *texteView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, strong) AMapSearchAPI *search;

@end

@implementation MemberRoamingVC
{
    int isdw;
    NSMutableArray *data;
 }
- (void)viewDidLoad {
    [super viewDidLoad];
    _map.delegate = self;
    _map.showsUserLocation = YES;
    _fview.layer.cornerRadius = 15;
    _search = [[AMapSearchAPI alloc]init];
    _table.delegate = self;
    _table.dataSource = self;
    self.search.delegate = self;
}
#pragma  mark 高德
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (updatingLocation)
    {
        if (isdw==0) {
            mapView.centerCoordinate = userLocation.location.coordinate;
            isdw = 1;
        }
    }
}
#pragma  mark 搜索
- (IBAction)selectAction:(UIButton *)sender {
    if([_textField.text trim].length == 0){
        return;
    }
    AMapInputTipsSearchRequest *request = [[AMapInputTipsSearchRequest alloc] init];
    request.keywords = _textField.text;
    request.city = [UserDefaults readUserDefaultObjectValueForKey:@"yimicity"];
    request.cityLimit = YES;
    //发起附近周边搜索
    [_search AMapInputTipsSearch:request];

}
/* 输入提示回调. */
- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
{
    data = response.tips;
    [_table reloadData];
    _table.hidden = NO;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return data.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    AMapTip *tip =[data objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@%@",tip.district,tip.name];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AMapTip *tip =[data objectAtIndex:indexPath.row];

    if (pointAnnotation) {
        [_map removeAnnotation:pointAnnotation];
    }
    coor.latitude = tip.location.latitude;
    coor.longitude = tip.location.longitude;
    pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = coor;
    pointAnnotation.title = tip.district;
    pointAnnotation.subtitle = tip.name;
    [_map addAnnotation:pointAnnotation];
    _table.hidden = YES;
    _map.centerCoordinate = coor;

}
-(void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate{
    if (pointAnnotation) {
        [mapView removeAnnotation:pointAnnotation];
    }
    pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = coordinate;
    coor = coordinate;
    CLLocation *c = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    CLGeocoder *revGeo = [[CLGeocoder alloc] init];
    [revGeo reverseGeocodeLocation:c
                 completionHandler:^(NSArray *placemarks, NSError *error) {
                     if (!error && [placemarks count] > 0)
                     {
                         NSDictionary  *dict = [[placemarks objectAtIndex:0] addressDictionary];
                         NSLog(@"定位成功-地址:%@ %f,%f",[dict objectForKey:@"Street"],coor.latitude,coor.longitude);
                         pointAnnotation.title = [dict objectForKey:@"SubLocality"];
                         pointAnnotation.subtitle = [dict objectForKey:@"Name"];
                         [mapView addAnnotation:pointAnnotation];
                     }}];
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    //  if ([annotation isKindOfClass:[MAAnnotationView class]]) {
    if (mapView.userLocation == annotation) {
        return nil;
    }
    static NSString *identify = @"annotation";
    //在原有的大头针中添加自定义的修饰
    MAPinAnnotationView *pointAnnotation = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identify];
    if (pointAnnotation == nil) {
        //在原有的大头针中创建一个新的自定义的大头针
        pointAnnotation = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identify];
    }
    //设置是否能选中的标题
    pointAnnotation.canShowCallout = YES;
    //是否允许拖拽
    pointAnnotation.draggable = YES;
    //是否允许退拽动画
    pointAnnotation.animatesDrop = YES;
    return pointAnnotation;
}

//- (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi*)mapPoi{
//    if (pointAnnotation) {
//        [_mapView removeAnnotation:pointAnnotation];
//    }
//    pointAnnotation = [[BMKPointAnnotation alloc]init];
//    coor.latitude = mapPoi.pt.latitude;
//    coor.longitude = mapPoi.pt.longitude;
//    CLLocation *c = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];
//    CLGeocoder *revGeo = [[CLGeocoder alloc] init];
//    [revGeo reverseGeocodeLocation:c
//                 completionHandler:^(NSArray *placemarks, NSError *error) {
//                     if (!error && [placemarks count] > 0)
//                     {
//                         NSDictionary  *dict = [[placemarks objectAtIndex:0] addressDictionary];
//                         NSLog(@"定位成功-地址:%@ %f,%f",[dict objectForKey:@"Street"],coor.latitude,coor.longitude);
//
//                         pointAnnotation.coordinate = coor;
//                         pointAnnotation.title = [dict objectForKey:@"SubLocality"];
//                         pointAnnotation.subtitle = [dict objectForKey:@"Street"];
//                         [_mapView addAnnotation:pointAnnotation];
//                     }}];
//
//}
-(void)viewWillAppear:(BOOL)animated
{
    //    [_mapView viewWillAppear];
    //    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
    //    [_mapView viewWillDisappear];
    //    _mapView.delegate = nil; // 不用时，置nil
}
- (IBAction)domeAction:(UIBarButtonItem *)sender {
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                 bundle:[NSBundle mainBundle]];
    MemberRoamingSVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MemberRoamingSVC"];
    searchResultDetailController.coor = coor;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}

- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
